const App = getApp()
import { AppModel } from '../../../../models/app.js'
const ModelApp = new AppModel()
import { addPreZero } from '../../../../utils/index.js'
 
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showModal: false,
    showShare: false,
    showNumber: false,
    redid: '',
    pid: '',
    packetSource: 1, //'红包来源（1：订单）'
    shareTitle: '',
    flag: 0,
    shareData: {},
    number: '',
    statusBarHeight: App.globalData.statusBarHeight,
    isTabbar:false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log('================== pay success ===============')
    console.log(options)
    // 上个页面进入
    this.data.pid = options.pid // 支付 paycode
    // 获取红包是否开启
    this.redFlag()
    //获取抽奖号码
    // this.getLuckydraw(options.pid)
    // if(options.number){
    //   this.setData({
    //     number: addPreZero(options.number),
    //     showNumber: true
    //   })
    // }
     // 隐藏右上角分享功能
    wx.hideShareMenu()
  },
  // 是否开始红包分享
  redFlag() {
    ModelApp.getRedFlag({})
      .then(res => {
        this.data.flag = res.data.red.useStatus // '订单红包是否启用 0 不用 1 用', && App.globalData.userInfo.userType != 1
        // 红包开启并且不是普通会员发红包
        if (this.data.flag == 1) {
          // 拿红包码
          this.sendRedPacket()
        }
      })
      .catch(err => {
        console.log('redFlag err:' + JSON.stringify(err))
      })
  },
  // 阻止滚动
  preventTouchMove() {
    return
  },
  //查看订单
  goOrder() {
    wx.redirectTo({
      url: '/pages/account/order/list/index?index=2'
    })
  },
  //回到首页
  goHome() {
    wx.redirectTo({
      url: '/pages/home/index'
    })
  },
  //关闭弹窗
  goClose() {
    this.setData({
      showModal: false
    })
  },
  // 弹出分享
  showShare() {
    this.setData({
      showShare: true,
      shareData: {
        shareTitle: '分享给你一个红包，快来领取吧!!!',
        redId: this.data.redid,
        sharePath: 'pages/redpacket/index'
      }
    })
  },
  //发红包
  sendRedPacket() {
    // 获取红包编码
    ModelApp.getRedPacket({
      packetSource: this.data.packetSource,
      payCode: this.data.pid
    })
      .then(res => {
        this.data.redid = res.data.packetCode
        // 显示发红包
        // this.setData({
        //   showModal: true
        // })
        // 显示红包
        this.showRed()
      })
      .catch(err => {
        console.log('sendRedPacket err:' + JSON.stringify(err))
      })
  },
  /* 转发*/
  onShareAppMessage: function(ops) {
    const _this = this
     const ucode = App.globalData.userInfo.userCode
    // const ucode = wx.getStorageSync('ucode')
    if (ops.from === 'button') {
      // 来自页面内转发按钮
      _this.path = `/pages/redpacket/index?bossid=${ucode}&redid=${_this.data
        .redid}`
      _this.title = `我下了一笔订单，分享一个红包，赶快去领取吧!`
      _this.imageUrl =
       '%ASSETS_IMG%/l-send-redpacket@2x.png?v=4'
    } else if (ops.from === 'menu') {
      // 如果后台关闭了分享支付成功那么就分享首页
      if (_this.data.flag == 0 || _this.data.redid == '') {
        _this.title = '小蚁买手'
        _this.path = `/pages/home/index?bossid=${ucode}`
        _this.img =  '%ASSETS_IMG%/l-banner@2x.png'
      } else {
        _this.title = '我下了一笔订单，分享一个红包，赶快去领取吧！'
        _this.path = `/pages/redpacket/index?bossid=${ucode}&redid=${_this.data
          .redid}`
        _this.imageUrl =
     '%ASSETS_IMG%/l-send-redpacket@2x.png?v=4'
      }
    }
    console.log('pay success path' + _this.path)
    return {
      title: _this.title,
      path: _this.path,
      imageUrl: _this.imageUrl,
      success: function(res) {
        // 转发成功
        console.log('转发成功:' + JSON.stringify(res))
      },
      fail: function(res) {
        // 转发失败
        console.log('转发失败:' + JSON.stringify(res))
      }
    }
  },
  //关闭抽奖码
  // closeshowNumber: function() {
  //   this.setData({
  //     showNumber: false
  //   }) 
  // },
  showRed() {
    // 用户必须是黄金黑金并且开启红包状态 && App.globalData.userInfo.userType != 1
    if (this.data.flag == 1 ) {
      this.setData({
        showModal: true
      })
    }
  },
  //获取抽奖号码
  // getLuckydraw: function(options) {
  //   const self = this
  //   const params = {
  //     payCode: options
  //   }

  //   ModelApp.selectActivityNumber(params)
  //     .then(res => {
  //       // 如果有抽奖号码
  //         if(res.data.number){
  //           self.setData({
  //             number: addPreZero(res.data.number),
  //             showNumber: true
  //           })
  //         }
  //     })
  //     .catch(err => {
  //       console.log(err)
  //     })
  // }
})
